@extends('layouts.application')

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品分类管理</strong> /
                    <small>Good categories Manage</small>
                </div>
            </div>

            <hr>
            @include('layouts._flash')

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('categories.create')}}" class="am-btn am-btn-default"><span class="am-icon-plus"></span>
                                新增
                            </a>
                            <button type="button" class="am-btn am-btn-success" id="show_all"
                                    href="">
                                <span class="am-icon-arrows-alt"></span> 展开所有
                            </button>
                        </div>
                    </div>
                </div>


            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-id">编号</th>
                                <th>缩略图</th>
                                <th class="table-name" style="width: 20%;">分类名</th>
                                <th style="width: 20%;">分类商品</th>
                                <th class="table-is-show am-hide-sm-only">是否显示</th>
                                <th class="table-sort-order am-hide-sm-only" style="width:10%">排序</th>
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach ($categories as $category)
                                <tr data-id="{{$category->id}}" id="row_{{$category->id}}" class="xParent">
                                    <td>{{$category->id}}</td>
                                    <td>
                                        @if($category->thumb)<img src="{{$category->thumb}}" alt="" class="thumb" style="width:50px;">@endif
                                    </td>
                                    <td>

                                        {{$category->name}}
                                    </td>
                                    <td>
                                    </td>
                                    <td class="am-hide-sm-only">
                                        {!! is_something('is_show',$category) !!}
                                    </td>

                                    <td class="am-hide-sm-only">
                                        <input type="text" name="sort_order" class="am-input-sm"
                                               value="{{$category->sort_order}}">
                                    </td>


                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                                   href="{{route('categories.edit',$category->id)}}">
                                                    <span class="am-icon-pencil-square-o"></span> 编辑
                                                </a>

                                                <a class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"
                                                   href="{{route('categories.destroy', $category->id)}}"
                                                   data-method="delete"
                                                   data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                @foreach ($category->children as $c)
                                    <tr data-id="{{$c->id}}" class="xChildren child_row_{{$category->id}}" style="display:none;">
                                        <td>{{$c->id}}</td>
                                        <td>
                                            @if($c->thumb)<img src="{{$c->thumb}}" alt="" class="thumb" style="width:50px;">@endif
                                        </td>
                                        <td>
                                            {{ category_indent(2) }}
                                            {{$c->name}}
                                        </td>

                                        <td>
                                            <a class="am-badge am-badge-secondary"
                                          href="/lxSystem/products?category_id={{$c->id}}">查看商品</a>
                                        </td>

                                        <td class="am-hide-sm-only">
                                            {!! is_something('is_show',$c) !!}
                                        </td>

                                        <td class="am-hide-sm-only">
                                            <input type="text" name="sort_order" class="am-input-sm"
                                                   value="{{$c->sort_order}}">
                                        </td>

                                        <td>
                                            <div class="am-btn-toolbar">
                                                <div class="am-btn-group am-btn-group-xs">
                                                    <a class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                                       href="{{route('categories.edit',$c->id)}}">
                                                        <span class="am-icon-pencil-square-o"></span> 编辑
                                                    </a>

                                                    <a class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"
                                                       href="{{route('categories.destroy', $c->id)}}"
                                                       data-method="delete"
                                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                                        <span class="am-icon-trash-o"></span> 删除
                                                    </a>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            @endforeach
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection
@section('js')
    <script>
        $(function(){
            $(".sort_order").change(function(){
                var info={
                    id:$(this).parents("tr").data('id'),
                    sort_order:$(this).val()
                };

                $.ajax({
                    type:"PATCH",
                    url:"/lxSystem/categories/sort_order",
                    data:info
                });
            })
            $('.is_something').click(function(){
                var _this=$(this);
                var info={
                    id:_this.parents("tr").data('id'),
                    attr:_this.data('attr')
                }
                $.ajax({
                    type:"PATCH",
                    url:"/lxSystem/categories/is_something",
                    data:info,
                    success:function(){
                        _this.toggleClass('am-icon-check am-icon-close')
                    }
                });

            });
            $("tr.xParent").dblclick(function () {
                $(this).toggleClass('am-active');
                $(".child_" + this.id).toggle();
            });
            $("#show_all").click(function(){
                $("tr.xParent").toggleClass('am-active');
                $(".xChildren").toggle();
            })
        })
    </script>
@endsection

